//
//  DeliveryInfoViewController.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/23.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit
import Masonry

class DeliveryInfoViewController: UIViewController {

    let containerView: UIView = {
        let containerView = UIView()
        containerView.layer.cornerRadius = 8
        containerView.backgroundColor = .white
        return containerView
    }()

    lazy var infoListView: InfoListView = {
        let listView = InfoListView()
        listView.datasource = self
        return listView
    }()

//    let sections = [DeliveryInfoSection(), DeliveryInfoSection()]

    let confirmButton: UIButton = {
        let button = UIButton()
        button.layer.cornerRadius = 8
        button.layer.masksToBounds = true
        button.setBackgroundImage(UIImage(color: .red), for: .normal)
        button.setTitle("知道了", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 14)
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor(white: 0, alpha: 0.3)

        view.addSubview(containerView)
        containerView.mas_updateConstraints { (make) in
            make?.left.equalTo()(view)?.offset()(30)
//            make?.top.equalTo()(view)?.offset()(30)
            make?.centerY.equalTo()(view)
            make?.right.equalTo()(view)?.offset()(-30)
            make?.height.equalTo()(view.mas_width)?.multipliedBy()(0.5)
        }

        containerView.addSubview(infoListView)
        let inset = UIEdgeInsetsMake(0, 30, 0, 30)
        infoListView.mas_makeConstraints { (make) in
            make?.edges.equalTo()(containerView)?.insets()(inset)
        }

        containerView.addSubview(confirmButton)
        confirmButton.mas_makeConstraints { (make) in
            make?.top.equalTo()(containerView.mas_bottom)?.offset()(10)
            make?.centerX.equalTo()(containerView)
            make?.width.equalTo()(240)
            make?.height.equalTo()(50)
        }

    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else { return }

        let location =  touch.location(in: view)
        if containerView.frame.contains(location) {
            super.touchesEnded(touches, with: event)
        } else {
            dismiss(animated: true, completion: nil)
        }
    }
}

extension DeliveryInfoViewController: InfoListViewDatasource {
    func listView(_ listVeiw: InfoListView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: InfoListRowsCell = listVeiw.tableView.dequeueReusableCell(for: indexPath)
        listVeiw.tableView.beginUpdates()
        cell.items = DeliveryInfoGroup().items
        listVeiw.tableView.endUpdates()
        return cell
    }

    func listView(_ listView: InfoListView, headerTextsInSection section: Int) -> [String] {
        return ["所涉订单"]
    }

    func numberOfSections(in listView: InfoListView) -> Int {
        return 3
    }
}
